﻿using System;
using System.Collections.Generic;
using System.Windows;


using Baosight.Platform.Core.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Utils;
using Telerik.Windows.Controls.Charting;
using Telerik.Windows.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCA.UI.Controls;
using Baosight.ZNH.ZMCA.UI.Model;
using System.Reflection;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Controls;


namespace Baosight.ZNH.ZMCA.UI
{


    public partial class ZMCA16 : EFView
    {

        ZMCA16Controler controler = new ZMCA16Controler();
        public ZMCA16()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(Platform.Core.EIInfo initData)
        {
            base.Initialize(initData);
            ZMCA03Controler.InitWarningPassRate(initData);

            //FillGaugeData();
            FillChartData();
            FillListViewData();
        }


        private void FillListViewData()
        {
            controler.GetListViewData(lstView);
        }


        public void FillChartData()
        {
            controler.GetDayChartData(this.chart);
            controler.getHoleTypeChart(this.holeTypeChart);
            //controler.GetMonthChartData(this.chartWorkface);
        }
    }

    public class ZMCA16Controler
    {
        RadChart monthTrendChart;
        RadChart dayTrendChart;
        RadChart holeChart;
        RadTreeListView lstView;

        static double maxValueRate = 1.5;

        public ZMCA16Controler()
        {
        }

        #region 
        /// <summary>
        /// 显示在页面左边，显示不同钻孔类型在当月的计划量和实际量
        /// 图形为柱状图
        /// </summary>
        /// <param name="chart"></param>
        public void getHoleTypeChart(RadChart chart)
        {
            this.holeChart = chart;

            //holeChart.DefaultView.ChartArea.NoDataString = "没有数据";

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA16");
            eiInfo.Set("method", "queryByHoleTypeChart");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, getHoleTypeChartDataCompleted);
        }

        private void getHoleTypeChartDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                Common.Alert(Common.MsgLoadDataError);
                return;
            }

            EIInfo eiInfo = e.EIInfo;
            EIBlock queryByChart = eiInfo.GetBlock("queryByHoleTypeChart");

            holeChart.SeriesMappings.Clear();
            holeChart.DefaultSeriesDefinition.ShowItemToolTips = true;
            holeChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

            this.holeChart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
            this.holeChart.DefaultView.ChartArea.AxisY.MinorGridLinesVisibility = Visibility.Visible;
            this.holeChart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Visible;

            this.holeChart.DefaultView.ChartTitle.FontSize = Common.FontsizeSetting + 2;
            this.holeChart.DefaultView.ChartArea.FontSize = Common.FontsizeSetting;
            this.holeChart.DefaultView.ChartLegend.FontSize = Common.FontsizeSetting - 2;
            this.holeChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = Common.ChartDefaultLabelFormat;

            string unit = "米";

            //暂时不显示计划值
            SeriesMapping sm1 = Common.AddBarToChart("计划", 0, "PlanValue");
            SeriesMapping sm2 = Common.AddBarToChart("实际", 0);
            sm1.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            sm2.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            holeChart.SeriesMappings.Add(sm1);
            holeChart.SeriesMappings.Add(sm2);

            StackPanel spLegendHeader = new StackPanel();
            spLegendHeader.Orientation = Orientation.Horizontal;
            TextBlock tbLegendHeader = new TextBlock();
            tbLegendHeader.Text = "月进度";
            tbLegendHeader.Foreground = new SolidColorBrush(Colors.White);
            spLegendHeader.Children.Add(tbLegendHeader);

            holeChart.DefaultView.ChartTitle.Content = spLegendHeader;
            holeChart.DefaultView.ChartLegend.Header = "单位：" + unit; ;


            List<List<ChartData>> itemsSource = new List<List<ChartData>>();
            List<ChartData> lstData = new List<ChartData>();

            foreach (var item in queryByChart.Rows)
            {
                if (Common.GetStringValue(item["fdWorkface"]).Trim() == string.Empty)
                {
                    continue;
                }
                ChartData cd = new ChartData(Common.GetStringValue(item["fdWorkface"]), 0);
                cd.PlanValue = Common.GetDecimalValue(item["fdPlanMine"]);
                cd.DataValue = Common.GetDecimalValue(item["fdActualSum"]);

                lstData.Add(cd);

            }
            itemsSource.Add(lstData);
            holeChart.ItemsSource = itemsSource;

        }
        #endregion

        /// <summary>
        /// 显示在页面右边，显示当月1号到今日的计划和实际量
        /// 图形为曲线
        /// </summary>
        /// <param name="chart"></param>
        public void GetDayChartData(RadChart chart)
        {
            this.dayTrendChart = chart;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA16");
            eiInfo.Set("method", "queryByDayChart");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, getDayChartDataCompleted);
        }

        private void getDayChartDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                Common.Alert(Common.MsgLoadDataError);
                return;
            }

            EIInfo eiInfo = e.EIInfo;
            EIBlock queryByChart = eiInfo.GetBlock("queryByDayChart");
            int currentMonth = Common.GetIntValue(eiInfo.Get("month"));
            int currentYear = Common.GetIntValue(eiInfo.Get("year"));
            int currentDay = Common.GetIntValue(eiInfo.Get("day"));
            int daysOfCurrentMonth = Common.GetIntValue(eiInfo.Get("daysOfCurrentMonth"));

            dayTrendChart.SeriesMappings.Clear();
            dayTrendChart.DefaultSeriesDefinition.ShowItemToolTips = true;
            dayTrendChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

            this.dayTrendChart.DefaultView.ChartArea.AxisY.MajorGridLinesVisibility = Visibility.Visible;
            this.dayTrendChart.DefaultView.ChartArea.AxisY.MinorGridLinesVisibility = Visibility.Visible;
            this.dayTrendChart.DefaultView.ChartArea.AxisY.StripLinesVisibility = Visibility.Visible;

            this.dayTrendChart.DefaultView.ChartTitle.FontSize = Common.FontsizeSetting + 2;
            this.dayTrendChart.DefaultView.ChartArea.FontSize = Common.FontsizeSetting;
            this.dayTrendChart.DefaultView.ChartLegend.FontSize = Common.FontsizeSetting - 2;
            this.dayTrendChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = Common.ChartDefaultLabelFormat;

            string unit = "米";

            SeriesMapping sm1 = Common.GenerateSeriesForChart("计划", 0, "Spline", "PlanValue");
            SeriesMapping sm2 = Common.GenerateSeriesForChart("实际", 0, "Spline");
            sm1.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            sm2.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            //(sm1.SeriesDefinition as SplineSeriesDefinition).ShowPointMarks = false;
            //(sm1.SeriesDefinition as SplineSeriesDefinition).ShowItemLabels = false;
            //(sm1.SeriesDefinition as SplineSeriesDefinition).ShowItemToolTips = false;

            dayTrendChart.SeriesMappings.Add(sm1);
            dayTrendChart.SeriesMappings.Add(sm2);

            StackPanel spLegendHeader = new StackPanel();
            spLegendHeader.Orientation = Orientation.Horizontal;
            TextBlock tbLegendHeader = new TextBlock();
            tbLegendHeader.Text = "日进度趋势";
            tbLegendHeader.Foreground = new SolidColorBrush(Colors.White);
            spLegendHeader.Children.Add(tbLegendHeader);
            dayTrendChart.DefaultView.ChartTitle.Content = spLegendHeader;
            //dayTrendChart.DefaultView.ChartTitle.Visibility = Visibility.Collapsed;
            dayTrendChart.DefaultView.ChartLegend.Header = "单位：" + unit;
            //dayTrendChart.DefaultView.ChartLegend.Header = string.Empty;
            //dayTrendChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Bottom;

            List<List<ChartData>> itemsSource = new List<List<ChartData>>();
            List<ChartData> lstData = new List<ChartData>();

            for (int i = 1; i <= currentDay; i++)
            {
                lstData.Add(new ChartData(i + "", 0));
            }

            //lstPlan.Add(new ChartData(" ", 0));
            //lstRate.Add(new ChartData("全年", 0));
            decimal monthPlan = 0;
            foreach (var item in queryByChart.Rows)
            {
                if (item["fdPlanMonth"].ToString().Trim() == string.Empty)
                {
                    continue;
                }
                else if (item["fdPlanMonth"].ToString().Trim() == "00")
                {
                    monthPlan = Common.GetDecimalValue(item["fdActualSum"]);
                    continue;
                }

                int day = Common.GetIntValue(item["fdPlanMonth"]);

                lstData[day - 1].DataValue = Common.GetDecimalValue(item["fdActualSum"]);
            }

            foreach (var item in lstData)
            {
                item.PlanValue = monthPlan / daysOfCurrentMonth;
            }
            itemsSource.Add(lstData);
            dayTrendChart.ItemsSource = itemsSource;
        }

        /// <summary>
        /// 用于首页，显示1月到当月的计划和实际量
        /// 图形为曲线
        /// </summary>
        /// <param name="chart"></param>
        public void GetMonthChartData(RadChart chart)
        {
            this.monthTrendChart = chart;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA16");
            eiInfo.Set("method", "queryByMonthChart");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, getMonthChartDataCompleted);
        }

        private void getMonthChartDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                Common.Alert(Common.MsgLoadDataError);
                return;
            }

            EIInfo eiInfo = e.EIInfo;
            EIBlock queryByMonthChart = eiInfo.GetBlock("queryByMonthChart");
            int currentMonth = Common.GetIntValue(eiInfo.Get("month"));
            int currentYear = Common.GetIntValue(eiInfo.Get("year"));


            monthTrendChart.SeriesMappings.Clear();
            monthTrendChart.DefaultSeriesDefinition.ShowItemToolTips = true;
            monthTrendChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
            this.monthTrendChart.DefaultView.ChartArea.AxisY.DefaultLabelFormat = Common.HomePageChartItemLabelFormat;

            string unit = "米";

            SeriesMapping sm1 = Common.GenerateSeriesForChart("计划", 0, "Spline", "PlanValue");
            SeriesMapping sm2 = Common.GenerateSeriesForChart("实际", 0, "Spline");
            sm1.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            sm2.SeriesDefinition.ItemLabelFormat = Common.ChartDefaultLabelFormat;
            monthTrendChart.SeriesMappings.Add(sm1);
            monthTrendChart.SeriesMappings.Add(sm2);

            //StackPanel spLegendHeader = new StackPanel();
            //spLegendHeader.Orientation = Orientation.Horizontal;
            //TextBlock tbLegendHeader = new TextBlock();
            ////tbLegendHeader.Text = currentYear + "年防突打钻趋势分析";
            //tbLegendHeader.Foreground = new SolidColorBrush(Colors.White);
            //spLegendHeader.Children.Add(tbLegendHeader);
            //chart.DefaultView.ChartTitle.Content = spLegendHeader;
            monthTrendChart.DefaultView.ChartTitle.Visibility = Visibility.Collapsed;
            //chart.DefaultView.ChartLegend.Header = "单位：" + unit;
            monthTrendChart.DefaultView.ChartLegend.Header = string.Empty;
            monthTrendChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Bottom;

            List<List<ChartData>> itemsSource = new List<List<ChartData>>();
            List<ChartData> lstData = new List<ChartData>();
            List<ChartData> lstRate = new List<ChartData>();

            for (int i = 1; i <= currentMonth; i++)
            {
                lstData.Add(new ChartData(i + "月", 0));
                lstRate.Add(new ChartData(i + "月", 0));
            }

            //lstPlan.Add(new ChartData(" ", 0));
            //lstRate.Add(new ChartData("全年", 0));

            //fdPlanType: 0年计划， 1月计划，20年实际， 30月实际， 40昨日
            foreach (var item in queryByMonthChart.Rows)
            {
                if (item["fdPlanYear"].ToString().Trim() == string.Empty)
                {
                    continue;
                }
                int dataType = Common.GetIntValue(item["fdPlanType"]);
                int month = Common.GetIntValue(item["fdPlanMonth"]);

                lstData[month - 1].PlanValue = Common.GetDecimalValue(item["fdPlanMine"]);
                lstData[month - 1].DataValue = Common.GetDecimalValue(item["fdActualSum"]);

                //将各月的值加到全年去

            }
            itemsSource.Add(lstData);

            monthTrendChart.ItemsSource = itemsSource;
        }

        //public static SeriesMapping AddSplineToChart(string label, int index)
        //{
        //    SeriesMapping sm = new SeriesMapping();
        //    SplineSeriesDefinition d2 = new SplineSeriesDefinition();
        //    d2.ItemLabelFormat = "P";
        //    d2.AxisName = "Secondary";
        //    sm.SeriesDefinition = d2;
        //    sm.LegendLabel = label;
        //    sm.CollectionIndex = index;
        //    sm.ItemMappings.Add(new ItemMapping("Date", DataPointMember.XCategory));
        //    sm.ItemMappings.Add(new ItemMapping("DataValue", DataPointMember.YValue));
        //    return sm;
        //}

        /// <summary>
        /// 将数据按照树形列表方式展现
        /// </summary>
        /// <param name="lstView"></param>
        public void GetListViewData(RadTreeListView lstView)
        {
            this.lstView = lstView;
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA16");
            eiInfo.Set("method", "queryByListView");

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, getListViewDataCompleted);
        }

        private void getListViewDataCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            if (e.EIInfo.Status == -1)
            {
                Common.Alert(Common.MsgLoadDataError);
                return;
            }

            EIInfo eiInfo = e.EIInfo;

            int currentMonth = Common.GetIntValue(eiInfo.Get("month"));
            int currentYear = Common.GetIntValue(eiInfo.Get("year"));

            for (int i = 12 - currentMonth + 2; i > 2; i--)
            {
                var cl = lstView.Columns[i];
                cl.IsVisible = false;
            }

            List<ZMCA03DetailItem> itemsSource = new List<ZMCA03DetailItem>();
            ZMCA03DetailItem yearItem = new ZMCA03DetailItem();

            itemsSource.Add(yearItem);
            Type itemType = yearItem.GetType();
            yearItem.Name = "全矿";


            //queryByWorkFace工作面汇总计划量，queryByWorkFaceA工作面汇总实际量
            //queryByDepartment单位汇总计划量，queryByDepartmentA单位汇总实际量



            ZMCA03DetailItem yearWorkFaceItem = new ZMCA03DetailItem();
            yearWorkFaceItem.Name = "所有工作面巷道";
            yearItem.Items.Add(yearWorkFaceItem);
            EIBlock queryByWorkFace = eiInfo.GetBlock("queryByWorkFace");
            EIBlock queryByWorkFaceA = eiInfo.GetBlock("queryByWorkFaceA");
            ZMCA03Controler.AddChildItemByBlock(queryByWorkFace, queryByWorkFaceA, yearWorkFaceItem, itemType, "fdDepartmentId", "fdWorkface");
            yearItem.CopyData(yearWorkFaceItem);


            ZMCA03DetailItem yearHoleTypeItem = new ZMCA03DetailItem();
            yearHoleTypeItem.Name = "所有钻孔类型";
            yearItem.Items.Add(yearHoleTypeItem);
            EIBlock queryByHoleType = eiInfo.GetBlock("queryByHoleType");
            EIBlock queryByHoleTypeA = eiInfo.GetBlock("queryByHoleTypeA");
            ZMCA03Controler.AddChildItemByBlock(queryByHoleType, queryByHoleTypeA, yearHoleTypeItem, itemType, "fdWorkface", "fdWorkface");

            ZMCA03DetailItem yearDepartmentItem = new ZMCA03DetailItem();
            yearDepartmentItem.Name = "所有单位";
            yearItem.Items.Add(yearDepartmentItem);
            EIBlock queryByDepartment = eiInfo.GetBlock("queryByDepartment");
            EIBlock queryByDepartmentA = eiInfo.GetBlock("queryByDepartmentA");
            ZMCA03Controler.AddDepartmentChildItemByBlock(queryByDepartment, queryByDepartmentA, yearDepartmentItem, itemType);

            lstView.ItemsSource = itemsSource;
        }

    }
}
